#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

int main()
{
    //创建套接字
    int sockfd = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
    if(sockfd < 0)
    {
        perror("sock");
        return 0;
    }
    //连接
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(27878);
    addr.sin_addr.s_addr = inet_addr("159.75.33.10");
    int ret = connect(sockfd,(struct sockaddr*)&addr,sizeof(addr));
    if(ret < 0)
    {
        perror("connect");
        return 0;
    }
    while(1)
    {
        //发送和接收
        char buf[1024] = {0};
        strcpy(buf,"i am client");
        ret = send(sockfd,buf,sizeof(buf)-1,0);
        if(ret < 0)
        {
            perror("send");
            continue;
        }
        memset(buf,'\0',sizeof(buf));
        ssize_t recv_size = recv(sockfd,buf,sizeof(buf)-1,0);
        if(recv_size < 0)
        {
            perror("recv");
            continue;
        }
        else if(recv_size == 0)
        {
            printf("peer close connect\n");
            close(sockfd);
            return 0;
        }
        printf("recv \"%s\"\n",buf);
        sleep(1);

    }
    close(sockfd);
    return 0;
}
